#include "tetris.h"

#include "board.h"
#include "brick.h"

int check_collision_down(void* _board, void* _brick, int brick_x, int brick_y)
{
    struct tetris_board* board = (struct tetris_board*)_board;
    struct brick_info* brick = (struct brick_info*)_brick;
    int x, y, board_x, board_y;
    int result = 0;
    for (y = 0; y < BRICK_RANGE; y++) {
        for (x = 0; x < BRICK_RANGE; x++) {
            if (brick->shape[y][x] != 0) {
                board_x = x + brick_x;
                board_y = y + brick_y;
                if (board_y >= TILE_Y - 1) {
                    result = 1;
                    goto Exit0;
                }

                if (board->tile_buf[board_y + 1][board_x].exist != 0) {
                    result = 1;
                    goto Exit0;
                }
            }
        }
    }
Exit0:
    return (result);
}

int check_collision_left(void* _board, void* _brick, int brick_x, int brick_y)
{
    struct tetris_board* board = (struct tetris_board*)_board;
    struct brick_info* brick = (struct brick_info*)_brick;
    int x, y, board_x, board_y;
    int result = 0;
    for (y = 0; y < BRICK_RANGE; y++) {
        for (x = 0; x < BRICK_RANGE; x++) {
            if (brick->shape[y][x] != 0) {
                board_x = x + brick_x;
                board_y = y + brick_y;
                if (board_x <= 0) {
                    result = 1;
                    goto Exit0;
                }

                if (board->tile_buf[board_y][board_x - 1].exist != 0) {
                    result = 1;
                    goto Exit0;
                }
            }
        }
    }
Exit0:
    return (result);
}

int check_collision_right(void* _board, void* _brick, int brick_x, int brick_y)
{
    struct tetris_board* board = (struct tetris_board*)_board;
    struct brick_info* brick = (struct brick_info*)_brick;
    int x, y, board_x, board_y;
    int result = 0;
    for (y = 0; y < BRICK_RANGE; y++) {
        for (x = 0; x < BRICK_RANGE; x++) {
            if (brick->shape[y][x] != 0) {
                board_x = x + brick_x;
                board_y = y + brick_y;
                if (board_x >= TILE_X - 1) {
                    result = 1;
                    goto Exit0;
                }

                if (board->tile_buf[board_y][board_x + 1].exist != 0) {
                    result = 1;
                    goto Exit0;
                }
            }
        }
    }
Exit0:
    return (result);
}
